package com.jeasonzhao.report.model.simpledb;

import com.jeasonzhao.model.Model;
import com.jeasonzhao.model.ModelException;
import com.jeasonzhao.model.ModelField;
import com.jeasonzhao.model.ModelHelper;
import com.jeasonzhao.report.model.Report;

/**
 * Create table SQLRPT_REPORT
 * (
 * 	id varchar(64) not null primary key,
 * 	title varchar(255) not null,
 * 	parentId varchar(64) null,
 * 	userid varchar(128) null,
 * 	tags varchar(128) null,
 * 	xmlContent text not null
 * );
 **/
@Model()
public class DbReport
{
    @ModelField
    private String id = null;
    @ModelField
    private String title = null;
    @ModelField
    private String parentId = null;
    @ModelField
    private String userId = null;
    @ModelField
    private String tags = null;
    @ModelField
    private String xmlContent = null;
    public DbReport()
    {

    }

    public Report toStandardObject()
        throws ModelException
    {
        Report rpt = ModelHelper.getInstance().fromXML(Report.class,this.xmlContent);
        if(null == rpt)
        {
            return null;
        }
        else
        {
            rpt.setReportId(this.id);
            rpt.setTitle(this.title);
            rpt.setParentReportId(this.parentId);
            rpt.setUserId(this.userId);
            rpt.setTags(this.tags);
            return rpt;
        }
    }

    public static DbReport fromStandardObject(Report report)
        throws ModelException
    {
        if(null == report)
        {
            return null;
        }
        else
        {
            DbReport db = new DbReport();
            db.id = report.getReportId();
            db.title = report.getTitle();
            db.parentId = report.getParentReportId();
            db.userId = report.getOwner();
            db.xmlContent = ModelHelper.getInstance().toXML(report).toString();
            db.tags = report.getTags();
            return db;
        }
    }
}
